﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KBC_Quiz_Show.DBConnect;
using System.Data.SqlClient;
using KBC_Quiz_Show.Object;
using KBC_Quiz_Show.Presentation;
using System.Collections;
using System.Data;

namespace KBC_Quiz_Show.Bussiness
{
    class AdminController
    {
        Utils database = new Utils();
        DataSet ds;
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlConnection conn;
        ArrayList list = new ArrayList();
        ArrayList listAdmin = new ArrayList();
        SqlDataReader reader, reader1;
        public ArrayList LoadAmin()
        {
            Admin admin;
            conn = database.getConnection();
            conn.Open();
            cmd = new SqlCommand("GetAdmin", conn);
            da = new SqlDataAdapter(cmd);
            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                int u_id = reader.GetInt32(0);
                String userName = reader.GetString(1);
                String pass = reader.GetString(2);
                admin = new Admin(userName, pass, u_id);
                list.Add(admin);
            }
            return list;
        }
        public void ChangePass(String username, String pass)
        {
            conn = database.getConnection();
            conn.Open();
            cmd = new SqlCommand("changePass", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
            cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = pass;
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        public ArrayList LoadAdminByUser(String username)
        {
            Admin admin;
            conn = database.getConnection();
            conn.Open();
            cmd = new SqlCommand("GetAdminByUser", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
            da = new SqlDataAdapter(cmd);
            reader1 = cmd.ExecuteReader();
            while (reader1.Read())
            {
                int uid = reader1.GetInt32(0);
                String userName = reader1.GetString(1);
                String pass = reader1.GetString(2);
                admin = new Admin(userName, pass, uid);
                listAdmin.Add(admin);
            }
            reader1.Close();
            return listAdmin;
            conn.Close();
        } 
    }
}
